<?php
namespace App\Support;

use Illuminate\Support\Facades\Redis as lRedis;

class Redis {
	public function exists($key) {
		return lRedis::EXISTS($key);
	}

	public function set($key, $value, $minutes = 0) {
		lRedis::SET($key, $value);
		if ($minutes > 0) {
			lRedis::EXPIRE($key, $minutes * 60);
		}
	}

	public function get($key) {
		return lRedis::GET($key);
	}

	public function del($key) {
		return lRedis::DEL($key);
	}

	public function hMset($key, $list, $minutes = 0) {
		lRedis::HMSET($key, $list);
		if ($minutes > 0) {
			lRedis::EXPIRE($key, $minutes * 60);
		}
	}

	public function hmGet($key, $fields = "") {
		if (!$fields) {
			return lRedis::HGETALL($key);
		}
		return $this->formatHmGet(lRedis::HMGET($key, $fields), $fields);
	}

	private function formatHmGet($list, $fields) {
		$data = [];
		foreach ($fields as $key => $value) {
			$data[$value] = $list[$key];
		}
		return $data;
	}
}